<template>
    <el-container class="home-container">
<!--      头部区域-->
      <el-header>
        <span>电商后台管理系统</span>
        <el-button @click="logout">退出</el-button>
      </el-header>
<!--      本体-->
      <el-container>
<!--        左边栏-->
        <el-aside :width="isCollapse?'64px':'200px'" >
          <div class="toggle-button" @click="toggleCollapse">|||</div>
          <el-menu
            background-color="rgb(51,55,68)"
            text-color="#fff"
            active-text-color="#ffd04b"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
<!--            一级导航-->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template #title>
                <i :class="iconsObj[item.id]"></i>
                <span>{{item.authName}}</span>
              </template>
<!--              二级导航-->
              <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="saveNavState('/'+subItem.path)">
                  <i class="el-icon-menu"></i>
                  <span>{{subItem.authName}}</span>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
<!--        正文-->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      menuList: null,
      iconsObj: {
        // 键名是id,对应键获取图标
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      activePath: ''
    }
  },
  created () {
    // 创建导航栏后获取数据
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout () {
      // 注销则删除token
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList () {
      // 获取菜单数据
      const { data } = await this.$http.get('menus')
      // 失败反馈报错信息
      if (data.meta.status !== 200) return this.$message.error(data.meta.message)
      // 成功就保存数据到数组中
      this.menuList = data.data
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    saveNavState (path) {
      window.sessionStorage.setItem('activePath', path)
      this.activePath = path
    }
  }
}
</script>

<style scoped>
.home-container{
  height: 100%;
}
  .el-header{
    background: rgb(55,61,65);
    color: white;
    font-size: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .el-aside{
    background: rgb(51,55,68);
  }
  .el-main{
    background: rgb(234,237,241);
  }
  .el-menu{
    border-right: 0;
  }
  .iconfont{
    margin-right: 10px;
  }
  .toggle-button{
    background-color: rgb(74,80,100);
    color: white;
    text-align: center;
    cursor: pointer;
    font-size: 10px;
    line-height: 24px;
    letter-spacing: 0.2em;
  }
</style>
